package com.uweic.flutter_wisdomscenic;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.tencent.mm.opensdk.modelpay.PayReq;
import com.uweic.flutter_wisdomscenic.R;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button login;
    // 微信登录
    private static IWXAPI WXapi;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WXapi = WXAPIFactory.createWXAPI(this,Constants.WX_APP_ID, true);
        WXapi.registerApp(Constants.WX_APP_ID);
        //绑定微信登录点击事件
        bindWxLoginClick();
        //绑定微信支付点击事件
        bindWxPayClick();
    }



    private void bindWxLoginClick(){
        login = (Button) findViewById(R.id.button);
        login.setOnClickListener(this);
    }

    private  void bindWxPayClick(){
        login = (Button) findViewById(R.id.button2);
        login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                WXLogin();
                break;
            case R.id.button2:
                wxPay();
                break;
            default:
                break;
        }
    }

    /**
     * 登录微信
     */
    private void WXLogin() {
        SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "wechat_sdk_demo";
        WXapi.sendReq(req);
    }


    /**
     * 唤起微信支付
     */
    private void wxPay() {
        PayReq request = new PayReq();
        request.appId = Constants.WX_APP_ID;
        //商户号
        request.partnerId = Constants.WX_MCH_ID;
        //预支付交易会话ID
        request.prepayId= Constants.WX_PREPAYID;
        request.packageValue = "Sign=WXPay";
        //随机字符串
        request.nonceStr= "009";
        request.timeStamp= "1398746502";
        //签名方式一定要与统一下单接口使用的一致
        request.sign= Constants.SIGN;
        WXapi.sendReq(request);
    }

}